//
//  CollectionViewController.m
//  HeartOfMeet
//
//  Created by qianfeng on 16/4/9.
//  Copyright © 2016年 千云锋. All rights reserved.
//

#import "CollectionViewController.h"
@interface CollectionViewController ()<UITableViewDelegate,UITableViewDataSource>


@end
@implementation CollectionViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
    self.title = @"我的收藏";
    
    [self navigationSetting];
    
//    self.dataArray = [[NSMutableArray alloc] initWithArray:[CollectionModel MR_findAll]];
    
    [self createTableView];
    
    [self editViewSetting];
}

- (void)navigationSetting{
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[[[UIImage imageNamed: @"white_back"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(back)];
    
    UIBarButtonItem *rightBarButtonItem1 = [[UIBarButtonItem alloc] initWithImage:[[[UIImage imageNamed: @"yinle_"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(pushPlayer)];
    
    self.rightBarButtonItem2 = [[UIBarButtonItem alloc] initWithImage:[[[UIImage imageNamed: @"delete_"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(delete:)];
    
    self.navigationItem.rightBarButtonItems = @[rightBarButtonItem1,self.rightBarButtonItem2];
}

- (void)back{
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)pushPlayer{
    [UIView  beginAnimations:nil context:NULL];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
    [UIView setAnimationDuration:0.25];
    [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:self.navigationController.view cache:NO];
    [UIView commitAnimations];
    
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDelay:0.1];
    [self.navigationController pushViewController:[PlayerViewController sharePlayer] animated:NO];
    [UIView commitAnimations];
}

- (void) editViewSetting{
    self.editView = [[UIView alloc] initWithFrame:CGRectMake(0, self.view.bounds.size.height-64, self.view.bounds.size.width, 39)];
    self.editView.backgroundColor = [UIColor darkGrayColor];
    
    self.cancel = [[UIButton alloc] initWithFrame:CGRectMake(2, 2, self.view.bounds.size.width/3, 35)];
    self.cancel.backgroundColor = [UIColor grayColor];
    [self.cancel setTitle:@"取消" forState:UIControlStateNormal];
    [self.cancel addTarget:self action:@selector(cancelClick) forControlEvents:UIControlEventTouchUpInside];
    [self.editView addSubview:self.cancel];
    
    self.delete = [[UIButton alloc] initWithFrame:CGRectMake(self.view.bounds.size.width/3 * 2 -2,  2, self.view.bounds.size.width/3, 35)];
    self.delete.backgroundColor = [UIColor grayColor];
    [self.delete setTitle:@"删除" forState:UIControlStateNormal];
    [self.delete addTarget:self action:@selector(deleteClick) forControlEvents:UIControlEventTouchUpInside];
    [self.editView addSubview:self.delete];
    
    [self.view addSubview:self.editView];
}

//编辑按钮
- (void)delete:(UIBarButtonItem *)item{
    self.rightBarButtonItem2.enabled = NO;
    
    self.tableView.allowsMultipleSelectionDuringEditing = YES;
    self.tableView.allowsSelectionDuringEditing = YES;
    [self.tableView setEditing:YES animated:YES];
    
    CGFloat y = self.editView.frame.origin.y - 39;
    [UIView animateWithDuration:0.2 animations:^{
        self.editView.frame = CGRectMake(0, y, self.view.bounds.size.width, 39);
    }];
}

//取消
- (void)cancelClick{
    self.tableView.allowsMultipleSelectionDuringEditing = NO;
    self.tableView.allowsSelectionDuringEditing = NO;
    [self.tableView setEditing:NO animated:YES];
    
    CGFloat y = self.editView.frame.origin.y + 39;
    [UIView animateWithDuration:0.2 animations:^{
        self.editView.frame = CGRectMake(0, y, self.view.bounds.size.width, 35);
    }];
    self.rightBarButtonItem2.enabled = YES;
}
//删除
- (void)deleteClick{
    NSArray *deleteArray = self.tableView.indexPathsForSelectedRows;
    
    NSArray *sortRows = [deleteArray sortedArrayUsingComparator:^NSComparisonResult(id obj1,id obj2){
        NSIndexPath *indexPath1 = obj1;
        NSIndexPath *indexPath2 = obj2;
        if (indexPath1.row > indexPath2.row) {
            return NO;
        }else{
            return YES;
        }
    }];
    
    for (NSIndexPath *indexPath in sortRows) {
        CollectionModel *model = self.dataArray[indexPath.row];
        NSArray *arr = [CollectionModel MR_findByAttribute:@"currentMp3Url" withValue:model.currentMp3Url];
        for (CollectionModel *model in arr) {
            [model MR_deleteEntity];
        }
        [[NSManagedObjectContext MR_defaultContext] MR_saveToPersistentStoreAndWait];
        [self.dataArray removeObjectAtIndex:indexPath.row];
    }
    [self.tableView deleteRowsAtIndexPaths:sortRows withRowAnimation:UITableViewRowAnimationRight];
    
    self.tableView.allowsMultipleSelectionDuringEditing = NO;
    self.tableView.allowsSelectionDuringEditing = NO;
    [self.tableView setEditing:NO animated:YES];
    
    CGFloat y = self.editView.frame.origin.y + 39;
    [UIView animateWithDuration:0.2 animations:^{
        self.editView.frame = CGRectMake(0, y, self.view.bounds.size.width, 35);
    }];
    self.rightBarButtonItem2.enabled = YES;
}

#pragma mark - tableView创建
- (void)createTableView{
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height-64) style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.backgroundColor = [UIColor blackColor];
    self.tableView.showsVerticalScrollIndicator = NO;
    [self.tableView registerNib:[UINib nibWithNibName:@"LeaderCell" bundle:nil] forCellReuseIdentifier:@"LeaderCell"];
    
    [self.view addSubview:self.tableView];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 65;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    LeaderTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"LeaderCell"];
    cell.backgroundView.backgroundColor = [UIColor clearColor];
    cell.selectionStyle = UITableViewCellSelectionStyleDefault;
    UIView *view = [[UIView alloc] initWithFrame:cell.frame];
    view.backgroundColor = [UIColor colorWithRed:0.3 green:0.3 blue:0.3 alpha:1];
    cell.selectedBackgroundView = view;
    
    CollectionModel *model = self.dataArray[indexPath.row];
    cell.TitleLabe.text = model.topicTitle;
    cell.NameLabel.text = model.speakNickName;
    
    [cell.CoverImageView sd_setImageWithURL:[NSURL URLWithString:model.mainImageUrl] placeholderImage:[UIImage imageNamed: @"placehold_rec"]];
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (self.tableView.editing == YES) {
        return;
    }
    
    
    CollectionModel *model = self.dataArray[indexPath.row];
    
    LeaderTableViewCell *cell = (LeaderTableViewCell *)[tableView cellForRowAtIndexPath:indexPath];
    CGFloat y = cell.frame.origin.y - tableView.contentOffset.y;
    [LZXHelper animationPlayWithUrl:model.mainImageUrl viewController:self leaderFrame:CGRectMake(8, y+64+7, 50, 50) newFrame:CGRectMake(self.view.bounds.size.width-30, 30, 6, 6)];
    
    [PlayerViewController sharePlayer].mainImageUrl = model.mainImageUrl;
    [PlayerViewController sharePlayer].topicTitle = model.topicTitle;
    [PlayerViewController sharePlayer].viewNum = model.viewNum;
    [PlayerViewController sharePlayer].avaImageUrl = model.avaImageUrl;
    [PlayerViewController sharePlayer].speakNickName = model.speakNickName;
    
    [[PlayerViewController sharePlayer] playWithUrl:model.currentMp3Url];
}



@end
